<?php
/**
*
* @package phpBB3
* @version $Id: chat.php 52 2007-11-04 05:56:17Z Handyman $
* @copyright (c) 2007 StarTrekGuide
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_chat.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/chat_mod');

$chat = new chat();
$chat_config = $chat->get_chat_config();

$chat->check_errors();

/******************************************/
/* EDIT these for custom online settings */
/****************************************/
$session_time = 300;  // Does anyone know what this is for?

/*****************************************/
/* DO NOT EDIT ANYTHING BELOW THIS LINE */
/***************************************/
$default_delay = $chat_config['update_seconds'];
$postlimit = (int) $chat_config['archive_message_limit'];

//set status
$times = array(
	'online'	=> $chat_config['online_time'],
	'idle'		=> $chat_config['idle_time'],
	'offline'	=> $chat_config['offline_time'],
);
//set delay for each status
$delay = array(
	'online'	=> $chat_config['online_delay'],
	'idle'		=> $chat_config['idle_delay'],
	'offline'	=> $chat_config['offline_delay'],
);

$mode = request_var('mode', '');
$last_id = request_var('last_id', 0);
$last_post = request_var('last_post', 0);
$last_time = request_var('last_time', 0);
$get = $init = false;
$count = 0;
$sql = 'SELECT * FROM ' . CHAT_TABLE . ' ORDER BY message_id DESC';
$result = $db->sql_query($sql);
$rows = $db->sql_fetchrowset($result);
$last_post_id = count($rows);

$data = $db->sql_query("SELECT * FROM ".CHAT_TABLE);
$last = ceil($tcount/$postlimit);
$pagenumber = $_REQUEST['start'];

switch ($mode)
{
	default:
		if(isset($_REQUEST['start']))
		{
			$sql = 'SELECT * FROM ' . CHAT_TABLE . ' WHERE message_id >= ' . (($last_post_id - $pagenumber) - $postlimit) . ' AND message_id <= ' . ($last_post_id - $pagenumber) . ' ORDER BY message_id DESC';
		}
		else
		{
			$sql = 'SELECT * FROM ' . CHAT_TABLE . ' WHERE message_id >= ' . ($last_post_id - $postlimit) . ' ORDER BY message_id DESC';
		}

		$result = $db->sql_query($sql);
		$rows = $db->sql_fetchrowset($result);
		foreach ($rows as $row)
		{
			if ($count++ == 0)
			{
				$last_id = $row['message_id'];
			}
			$template->assign_block_vars('chatrow', array(
				'MESSAGE_ID'	=> $row['message_id'],
				'USERNAME_FULL'	=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $user->lang['GUEST']),
				'MESSAGE'		=> generate_text_for_display($row['message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']),
				'TIME'			=> $user->format_date($row['time']),
				'CLASS'			=> ($row['message_id'] % 2) ? 1 : 2,
			));
		}
		$db->sql_freeresult($result);
		if ($user->data['user_type'] == USER_FOUNDER || $user->data['user_type'] == USER_NORMAL)
		{
			$sql = 'SELECT * FROM ' . CHAT_SESSIONS_TABLE . " WHERE user_id = {$user->data['user_id']}";
			$result = $db->sql_query($sql);
			$row = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);
			if ($row['user_id'] != $user->data['user_id'])
			{
				$sql_ary = array(
					'user_id'			=> $user->data['user_id'],
					'username'			=> $user->data['username'],
					'user_colour'		=> $user->data['user_colour'],
					'user_login'		=> time(),
					'user_lastupdate'	=> time(),
				);
				$sql = 'INSERT INTO ' . CHAT_SESSIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
				$db->sql_query($sql);
			}
			else
			{
				$sql_ary = array(
					'username'			=> $user->data['username'],
					'user_colour'		=> $user->data['user_colour'],
					'user_login'		=> time(),
					'user_lastupdate'	=> time(),
				);
				$sql = 'UPDATE ' . CHAT_SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE user_id = {$user->data['user_id']}";
				$db->sql_query($sql);
			}
		}
		$chat->whois_online();
		$template->assign_vars(array(
			'TIME'	=> time(),
			'DELAY'	=> $default_delay,
		));
	break;
	case 'read':
		$sql = 'SELECT * FROM ' . CHAT_TABLE . " WHERE message_id < ".$_REQUEST['page']." ORDER BY message_id DESC";
		$result = $db->sql_query($sql);
		$rows = $db->sql_fetchrowset($result);
		if (!sizeof($rows) && ((time() - 60) < $last_time))
		{
			exit;
		}
		foreach ($rows as $row)
		{
			if ($count++ == 0)
			{
				$last_id = $row['message_id'];
                $template->assign_block_vars('soundrow', array(
                'SOUND'   => '<div style="visibility: hidden; position: absolute;"><object data="sound.swf" type="application/x-shockwave-flash"><param name="movie" value="sound.swf" /></object></div>',
                ));
			}
			$template->assign_block_vars('chatrow', array(
				'MESSAGE_ID'	=> $row['message_id'],
				'USERNAME_FULL'	=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $user->lang['GUEST']),
				'MESSAGE'		=> generate_text_for_display($row['message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']),
				'TIME'			=> $user->format_date($row['time']),
				'CLASS'			=> ($row['message_id'] % 2) ? 1 : 2,
			));
		}
		$db->sql_freeresult($result);
		if ((time() - 60) > $last_time)
		{
			$chat->whois_online();
			$sql_ary = array(
				'username'			=> $user->data['username'],
				'user_colour'		=> $user->data['user_colour'],
				'user_lastupdate'	=> time(),
			);
			$sql = 'UPDATE ' . CHAT_SESSIONS_TABLE . '
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
				WHERE user_id = {$user->data['user_id']}";
			$result = $db->sql_query($sql);
		}
		$get = true;
	break;
	case 'add':
		if (!$user->data['is_registered'] || $user->data['user_type'] == USER_INACTIVE || $user->data['user_type'] == USER_IGNORE)
		{
			redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'));
		}
		$get = true;
		$read_interval = request_var('read_interval', 0);
		$message = utf8_normalize_nfc(request_var('message', '', true));
		if (!$message)
		{
			break;
		}
		$message = str_replace('---', '- -', $message);
		$uid = $bitfield = $options = '';
		$allow_bbcode = $allow_urls = $allow_smilies = true;
		generate_text_for_storage($message, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);
		$sql_ary = array(
			'chat_id'			=> 1,
			'user_id'			=> $user->data['user_id'],
			'username'			=> $user->data['username'],
			'user_colour'		=> $user->data['user_colour'],
			'message'			=> $message,
			'bbcode_bitfield'	=> $bitfield,
			'bbcode_uid'		=> $uid,
			'bbcode_options'	=> $options,
			'time'				=> time(),
		);
		$sql = 'INSERT INTO ' . CHAT_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
		$db->sql_query($sql);
		$sql_ary = array(
			'username'			=> $user->data['username'],
			'user_colour'		=> $user->data['user_colour'],
			'user_lastpost'		=> time(),
			'user_lastupdate'	=> time(),
		);
		$sql = 'UPDATE ' . CHAT_SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE user_id = {$user->data['user_id']}";
		$result = $db->sql_query($sql);
		$sql = 'SELECT * FROM ' . CHAT_TABLE . " WHERE message_id > $last_id ORDER BY message_id DESC";
		$result = $db->sql_query($sql);
		$rows = $db->sql_fetchrowset($result);
		if (!sizeof($rows) && ((time() - 60) < $last_time))
		{
			exit;
		}
		foreach ($rows as $row)
		{
			if ($count++ == 0)
			{
                $template->assign_block_vars('soundrow', array(
                'SOUND'   => '<div style="visibility: hidden; position: absolute;"><object data="soundout.swf" type="application/x-shockwave-flash"><param name="movie" value="soundout.swf" /></object></div>',
                ));
             $last_id = $row['message_id'];
             }
			$template->assign_block_vars('chatrow', array(
				'MESSAGE_ID'	=> $row['message_id'],
				'USERNAME_FULL'	=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $user->lang['GUEST']),
				'MESSAGE'		=> generate_text_for_display($row['message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']),
				'TIME'			=> $user->format_date($row['time']),
				'CLASS'			=> ($row['message_id'] % 2) ? 1 : 2,
			));
		}
		$db->sql_freeresult($result);
		if ($read_interval != $delay['online'])
		{
			whois_online();
		}
	break;
	case 'delete':
		$get = true;
		$chat_id = request_var('chat_id', 0);
		if (!$chat_id)
		{
			break;
		}
		if (!$auth->acl_get('a_') && !$auth->acl_get('m_'))
		{
			break;
		}
		$sql = 'DELETE FROM ' . CHAT_TABLE . " WHERE message_id = $chat_id";
		$db->sql_query($sql);
	break;
}

$mode = strtoupper($mode);

$template->assign_vars(array(
	'FILENAME'		=> append_sid("{$phpbb_root_path}chat.$phpEx"),
	'LAST_ID'		=> $last_id,
	'S_CHAT'		=> (!$get) ? true : false,
	'S_GET_CHAT'	=> ($get) ? true : false,
	'S_' . $mode	=> true,
));

$template->assign_block_vars('navlinks', array(
	'FORUM_NAME'	=> 'Chat Archive',
	'U_VIEW_FORUM'	=> append_sid("{$phpbb_root_path}chat_archive.$phpEx"))
);

page_header($user->lang['PAGE_TITLE']);

$template->assign_vars(array(
	'MAKE_FORUM_ACTIVE' => ' class="activetab"',
	'SUB_NAV' => 'sub_nav_forum',
  'PAGE'  => $_REQUEST['page'],
  'PAGINATION' => generate_pagination($_SERVER['PHP_SELF'], $last_post_id, $postlimit, $pagenumber, true),
));

$template->set_filenames(array(
	'body' => 'chat_archive_body.html')
);

page_footer();

?>